﻿//-----------------------------------------------------------------------
// <copyright file="ControlExtensions.cs" company="AFS">
// Copyright (c) AFS. All rights reserved.
//
//    This file is part of Chatbox.
//
//    Chatbox is free software: you can redistribute it and/or modify
//    it under the terms of the GNU Lesser General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    any later version.
//
//    Chatbox is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//    GNU Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public License
//    along with Chatbox. If not, see http://www.gnu.org/copyleft/lesser.html
// </copyright>
//----------------------------------------------------------------------- 

namespace ChatBox.UI.Controls
{
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;

    /// <summary>
    /// Extensions to visual controls
    /// </summary>
    public static class ControlExtensions
    {
        /// <summary>
        /// Is the scrollbar at the bottom?
        /// </summary>
        /// <param name="scroll">
        /// the scroll viewer to inspect
        /// </param>
        /// <returns>
        /// True if the scrollbar is at the bottom
        /// </returns>
        public static bool IsAtBottom(this ScrollViewer scroll)
        {
            if (scroll.ComputedVerticalScrollBarVisibility != Visibility.Visible)
            {
                // no scrollbar shown yet. At bottom
                return true;
            }

            // is it at the bottom?
            return scroll.VerticalOffset >= scroll.ScrollableHeight;
        }

        /// <summary>
        /// Find the scroll viewer in a FlowDocumentScrollViewer
        /// </summary>
        /// <param name="flowDocumentScrollViewer">the FlowDocumentScrollViewer to inspect</param>
        /// <returns>the contained ScrollViewer</returns>
        public static ScrollViewer FindScrollViewer(this DependencyObject flowDocumentScrollViewer)
        {
            if (VisualTreeHelper.GetChildrenCount(flowDocumentScrollViewer) == 0)
            {
                return null;
            }

            // Border is the first child of first child of a ScrolldocumentViewer
            DependencyObject firstChild = VisualTreeHelper.GetChild(flowDocumentScrollViewer, 0);
            if (firstChild == null)
            {
                return null;
            }

            Decorator border = VisualTreeHelper.GetChild(firstChild, 0) as Decorator;

            if (border == null)
            {
                return null;
            }

            return border.Child as ScrollViewer;
        }
    }
}